package com.zmc.search.controller;

import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.zmc.common.domain.es.EsSkuEntity;
import com.zmc.common.domain.query.collection.MyCollectionFeignQuery;
import com.zmc.common.domain.query.coupon.CouponEsFeignQuery;
import com.zmc.common.domain.query.goods.GoodDetailFeignQuery;
import com.zmc.common.domain.query.goods.GoodSkuFeignQuery;
import com.zmc.common.domain.query.goods.GoodSkuSearchFeignQuery;
import com.zmc.common.domain.query.home.*;
import com.zmc.common.domain.vo.good.GoodsDeleteVo;
import com.zmc.common.domain.vo.good.GoodsDownVo;
import com.zmc.common.utils.R;
import com.zmc.search.constant.EsConstant;
import com.zmc.search.service.ElasticsearchService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @Author：zmc
 * @Date：2024/10/15 15:27
 * ES存储商品类
 */

@RestController
@RequestMapping("/product")
public class EsProductController {

    @Autowired
    private ElasticsearchService elasticsearchService;

    /**
     * 上架商品
     */
    @PostMapping("/up")
    public R productUpToEs(@RequestBody List<EsSkuEntity> skuEsModels) {
        if (CollectionUtils.isNotEmpty(skuEsModels)) {
            elasticsearchService.addBatchData(EsConstant.PRODUCT_INDEX, skuEsModels);
        }
        return R.ok();
    }

    /**
     * 下架商品
     */
    @PostMapping("/down")
    public R productDownToEs(@RequestBody GoodsDownVo vo) {
        return R.ok(elasticsearchService.updateDataByCondition(EsConstant.PRODUCT_INDEX, vo,EsSkuEntity.class));
    }

    /**
     * 删除商品
     */
    @PostMapping("/delete")
    public R productDelete(@RequestBody GoodsDeleteVo vo) {
        return R.ok(elasticsearchService.deleteDataByCondition(EsConstant.PRODUCT_INDEX, vo));
    }

    /**
     * 首页大屏搜索
     * */
    @RequestMapping("/keyword")
    public R homeGoods(@RequestBody HomeKeywordFeignQuery query) {
        return R.ok(elasticsearchService.selectDataPage(EsConstant.PRODUCT_INDEX, query.getPageNum(), query.getPageSize(), query, EsSkuEntity.class));
    }

    /**
     * 热卖商品
     */
    @PostMapping("/hot")
    public R hotGoods(@RequestBody HomeHotFeignQuery query) {
        return R.ok(elasticsearchService.selectDataPage(EsConstant.PRODUCT_INDEX, query.getPageNum(), query.getPageSize(), query, EsSkuEntity.class));
    }

    /**
     * 按综合搜索
     * */
    @RequestMapping("/total")
    public R totalGoods(@RequestBody HomeTotalFeignQuery query) {
        return R.ok(elasticsearchService.selectDataPage(EsConstant.PRODUCT_INDEX, query.getPageNum(), query.getPageSize(), query, EsSkuEntity.class));
    }

    /**
     * 按销量搜索
     * */
    @RequestMapping("/sale")
    public R saleCountGoods(@RequestBody HomeSaleCountFeignQuery query) {
        return R.ok(elasticsearchService.selectDataPage(EsConstant.PRODUCT_INDEX, query.getPageNum(), query.getPageSize(), query, EsSkuEntity.class));
    }

    /**
     * 按价格搜索
     * */
    @RequestMapping("/price")
    public R priceGoods(@RequestBody HomeSalePriceFeignQuery query) {
        return R.ok(elasticsearchService.selectDataPage(EsConstant.PRODUCT_INDEX, query.getPageNum(), query.getPageSize(), query, EsSkuEntity.class));
    }

    /**
     * 获取我的收藏列表
     * */
    @RequestMapping("/collect")
    public R collectionList(@RequestBody MyCollectionFeignQuery query) {
        return R.ok(elasticsearchService.selectDataList(EsConstant.PRODUCT_INDEX, query, EsSkuEntity.class));
    }

    /**
     * 通过spuId获取详情页部分信息
     * */
    @PostMapping("/good/detail")
    public R getGoodsDetailBySpuId(@RequestBody GoodDetailFeignQuery query) {
        return R.ok(elasticsearchService.selectDataList(EsConstant.PRODUCT_INDEX, query, EsSkuEntity.class));
    }

    /**
     * 通过skuIds获取sku列表信息
     * */
    @PostMapping("/skus")
    public R getSkuInfosBySkuIds(@RequestBody GoodSkuFeignQuery query) {
        return R.ok(elasticsearchService.selectDataList(EsConstant.PRODUCT_INDEX, query, EsSkuEntity.class));
    }


    /**
     * 条件查询sku列表
     * */
    @PostMapping("/sku/list")
    public R skuSearch(@RequestBody GoodSkuSearchFeignQuery query) {
        return R.ok(elasticsearchService.selectDataPage(EsConstant.PRODUCT_INDEX, query.getPageNum(), query.getPageSize(), query, EsSkuEntity.class));
    }

    /**
     * 促销中心查询sku列表
     * */
    @PostMapping("/coupon/list")
    public R skuCouponSearch(@RequestBody CouponEsFeignQuery query) {
        return R.ok(elasticsearchService.selectDataPage(EsConstant.PRODUCT_INDEX, query.getPageNum(), query.getPageSize(), query, EsSkuEntity.class));
    }

}
